package com.bit.ai.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

@RequestMapping("/chat")
@RestController
public class ChatClientController {

    @Autowired
    private ChatClient chatClient;

//    public ChatClientController(ChatClient.Builder chatClientBuilder) {
//        this.chatClient = chatClientBuilder
//                // 设置系统提示词
//                .defaultSystem("你叫玄鉴，是关海鹏研发的一款智能AI助手，你擅长Java和C++，主要负责计算机学员的日常答疑，请以友好的态度来回答问题")
//                .build();
//    }

    @GetMapping("/call")
    public String generation(String userInput) {
        return this.chatClient.prompt()
                //用户输入
                .user(userInput)
                //调用大模型
                .call()
                //返回响应
                .content();
    }

    record Recipe(String dish, List<String> ingredients) {}

    @RequestMapping("/entity")
    public String entity(String userInput) {
        Recipe recipe = chatClient.prompt()
                .user(String.format("请帮我生成%s的食谱", userInput))
                .call()
                .entity(Recipe.class);
        return recipe.toString();
    }

    @GetMapping(value = "/stream", produces = "text/html;charset=utf-8")
    public Flux<String> stream(String userInput) {
        return this.chatClient.prompt()
                //用户输入
                .user(userInput)
                //调用大模型   流式输出
                .stream()
                //返回响应
                .content();
    }
}
